package com.ruoyi.apartments.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.apartments.mapper.ApartmentsMapper;
import com.ruoyi.apartments.domain.Apartments;
import com.ruoyi.apartments.service.IApartmentsService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 公寓管理Service业务层处理
 *
 * @author ruoyi
 * @date 2024-10-12
 */
@Service
public class ApartmentsServiceImpl implements IApartmentsService
{
    @Autowired
    private ApartmentsMapper apartmentsMapper;

    /**
     * 查询公寓管理
     *
     * @param apartmentId 公寓管理主键
     * @return 公寓管理
     */
    @Override
    public Apartments selectApartmentsByApartmentId(Long apartmentId)
    {
        return apartmentsMapper.selectApartmentsByApartmentId(apartmentId);
    }

    /**
     * 查询公寓管理列表
     *
     * @param apartments 公寓管理
     * @return 公寓管理
     */
    @Override
    public List<Apartments> selectApartmentsList(Apartments apartments)
    {
        return apartmentsMapper.selectApartmentsList(apartments);
    }

    /**
     * 新增公寓管理
     *
     * @param apartments 公寓管理
     * @return 结果
     */
    @Override
    @Transactional
    public int insertApartments(Apartments apartments)
    {

        int rows = apartmentsMapper.insertApartments(apartments);
        Long apartmentId = apartments.getApartmentId();
        List<Long> facilityIds = apartments.getFacilityIds();
        List<Long> tagIds = apartments.getTagIds();
        List<Long> feeIds = apartments.getFeeIds();
        if(apartments.getFacilityIds()!= null &&!apartments.getFacilityIds().isEmpty()){
                apartmentsMapper.insertApartmentFacilities(apartmentId, facilityIds);
        }
        if(apartments.getTagIds()!= null &&!apartments.getTagIds().isEmpty()){
            apartmentsMapper.insertApartmentTags(apartmentId, tagIds);
        }
        if(apartments.getFeeIds()!= null &&!apartments.getFeeIds().isEmpty()){
            apartmentsMapper.insertApartmentFees(apartmentId, feeIds);
        }
        return rows;
    }

    /**
     * 修改公寓管理
     *
     * @param apartments 公寓管理
     * @return 结果
     */
    @Override
    @Transactional
    public int updateApartments(Apartments apartments)
    {
        apartmentsMapper.deleteApartmentTags(apartments.getApartmentId());
        apartmentsMapper.deleteApartmentFacilities(apartments.getApartmentId());
        apartmentsMapper.deleteApartmentFees(apartments.getApartmentId());
        if(apartments.getFacilityIds()!= null &&!apartments.getFacilityIds().isEmpty()){
            apartmentsMapper.insertApartmentFacilities(apartments.getApartmentId(), apartments.getFacilityIds());
        }
        if(apartments.getTagIds()!= null &&!apartments.getTagIds().isEmpty()){
            apartmentsMapper.insertApartmentTags(apartments.getApartmentId(), apartments.getTagIds());
        }
        if(apartments.getFeeIds()!= null &&!apartments.getFeeIds().isEmpty()){
            apartmentsMapper.insertApartmentFees(apartments.getApartmentId(), apartments.getFeeIds());
        }
        return apartmentsMapper.updateApartments(apartments);
    }

    /**
     * 批量删除公寓管理
     *
     * @param apartmentIds 需要删除的公寓管理主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteApartmentsByApartmentIds(Long[] apartmentIds)
    {
        for (Long apartmentId : apartmentIds)
        {
            apartmentsMapper.deleteApartmentFacilities(apartmentId);
            apartmentsMapper.deleteApartmentTags(apartmentId);
        }
        return apartmentsMapper.deleteApartmentsByApartmentIds(apartmentIds);
    }

    @Override
    @Transactional
    public int insertApartmentFacilities(Apartments apartments) {

        List<Long> facilityIds = apartments.getFacilityIds();
        int rows = apartmentsMapper.insertApartmentFacilities(apartments.getApartmentId(),facilityIds);
        if (facilityIds != null && !facilityIds.isEmpty())
        {
            apartmentsMapper.insertApartmentFacilities(apartments.getApartmentId(), facilityIds);
        }
        return rows;
    }
    @Override
    @Transactional
    public void insertApartmentTags(Apartments apartments) {
        List<Long> tagIds = apartments.getTagIds();

        if (tagIds != null && !tagIds.isEmpty())
        {
            apartmentsMapper.insertApartmentTags(apartments.getApartmentId(), tagIds);
        }

    }

    @Override
    public void insertApartmentFees(Apartments apartments) {
        List<Long> feeIds = apartments.getFeeIds();
        if (feeIds != null && !feeIds.isEmpty())
        {
            apartmentsMapper.insertApartmentFees(apartments.getApartmentId(), feeIds);
        }
    }


}
